// OpenSuperModal  一键督办弹框
import { OneModal } from '@/components/one-ui';
import { IOneModalRef } from '@/pages/type';
import { Spin, message } from 'antd';
import { Ref, useRef } from 'react';
import { ContractManageAPI } from '../common/type';
import { batchHandleService } from '../services';
import { onCloseModalCommon, getUserID, getPosIDs, getUserDepID, getCompanyID } from '@/utils';

export const OpenSuperModal: React.FC<ContractManageAPI.IOpenSuperModal> = ({ onClose, selectRowInfo, reloadManage, setSelectRowKeys }) => {
  /** 弹框Ref */
  const performanceModalRef: Ref<IOneModalRef> = useRef(null);

  /** 一键督办回调 */
  const batchCallback = (res: any) => {
    if (res && res?.msgFlag) {
      message.success(res?.msg);
      onCloseModalCommon(performanceModalRef);
      setSelectRowKeys({ rowKeysList: [], valList: [] });
      reloadManage();
    } else {
      message.error(res?.msg);
    }
  };

  const { batchHandleRun, batchHandleLoading } = batchHandleService(batchCallback);

  /** 一键督办弹框 点击确定 */
  const onSave = () => {
    batchHandleRun({
      handleList: selectRowInfo?.map((item) => {
        return {
          contractID: item?.id,
          contractCode: item?.contractCode,
          handleInfo: `${item?.contractName}合同,（编号：${item?.contractCode}）扫描件上传已逾期，请您尽快完成上传。`,
          handleType: 1,
          readFlag: 0,
          handlerID: getUserID(),
          handlePositionID: getPosIDs(),
          handleDepartmentID: getUserDepID(),
          handleCompanyID: getCompanyID(),
          versionNum: 0,
        };
      }),
    });
  };

  /** 处理督办编号 */
  const handContractCode = () => {
    let codeStr: string = '';
    selectRowInfo?.forEach((item: any, index: number) => {
      codeStr += item?.contractCode + (selectRowInfo?.length - 1 == index ? '' : '，');
    });
    return codeStr;
  };

  return (
    <>
      <OneModal
        open={true}
        getContainer={false}
        ref={performanceModalRef}
        title={'确定提醒'}
        // width={document.body.clientWidth * 0.5}
        onCancel={onClose}
        bodyStyle={{
          padding: '15px',
        }}
        onOk={onSave}
        confirmLoading={batchHandleLoading}
      >
        <Spin spinning={batchHandleLoading}>
          <div style={{ fontSize: 20 }}>
            确定要将合同编号为【<span style={{ color: 'red' }}> {selectRowInfo?.length > 0 ? handContractCode() : '--'}</span>
            】的合同进行提醒！
          </div>
        </Spin>
      </OneModal>
    </>
  );
};
